package com.longdaji.decoration.ui.activitiesOfCommunity.publishDynamic;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.adapter.DynamicAdapter;
import com.longdaji.decoration.adapter.PhotoAdapter;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.ui.activitiesOfCommunity.publishDynamic.addLabel.AddLabelActivity;
import com.longdaji.decoration.ui.activitiesOfCommunity.publishDynamic.addTopic.AddTopicActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

/**
 * Created by Kanade on 2018/1/18 0018.
 */

public class PublishDynamicActivity extends BaseActivity implements PublishDynamicContract.View {
    @Inject
    PublishDynamicContract.Presenter mPresenter;

    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.rv_photo)
    RecyclerView rvPhoto;


    private static final int REQUEST_CODE_CHOOSE = 23;
    private PhotoAdapter mAdapter;
    private Context mContext;
    private List<Uri> mUriList = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_dynamic);
        setUnBinder(ButterKnife.bind(this));
        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("发布动态");

        GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return 1;
            }
        });
        rvPhoto.setLayoutManager(new GridLayoutManager(this, 4));
        mAdapter = new PhotoAdapter(mUriList);
        mAdapter.setAddPhotoListener(new PhotoAdapter.AddPhotoListener() {
            @Override
            public void addPhoto() {
                RxPermissions rxPermissions = new RxPermissions(PublishDynamicActivity.this);
                rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean aBoolean) throws Exception {
                                if (aBoolean) {
                                    Log.d("mytest", "add photo");
                                    Matisse.from(PublishDynamicActivity.this)
                                            .choose(MimeType.allOf())
                                            .countable(true)
                                            .theme(R.style.Matisse_Dracula)
                                            .maxSelectable(12)
                                            .imageEngine(new GlideEngine())
                                            .forResult(REQUEST_CODE_CHOOSE);
                                }
                            }
                        });
            }
        });
        rvPhoto.setAdapter(mAdapter);

    }

    @OnClick({R.id.rl_tool_back, R.id.ll_add_topic, R.id.ll_add_expression, R.id.ll_add_label})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            case R.id.ll_add_topic:
                startActivity(new Intent(this, AddTopicActivity.class));
                break;
            case R.id.ll_add_expression:
                break;
            case R.id.ll_add_label:
                startActivity(new Intent(this, AddLabelActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
            mUriList = Matisse.obtainResult(data);
            mAdapter.setData(mUriList);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }
}
